function [ x, best_k, best_t, closure, converge ] = SimpleIterationsMethod2( A, b, eps, max_iterations, t_min, t_max)
%Solves the system of linear equations
N = size(A, 1);
best_t = 0;
best_k =Inf;
step = (t_max - t_min) / 1000;

fileId = fopen('report.txt', 'w');

for t = t_min : step : t_max
    if (t == 0)
        continue;
    end;
    B = eye(N) - t * A;
    c = t * b;
    norm_no = 1;
    coef = 0;
    if (norm(B, norm_no) > 1)
        norm_no = 2;
        if (norm(B, norm_no) > 1)
            norm_no = Inf;
            if (norm(B, norm_no) > 1)
                norm_no = 1;
                coef = 1;
            end;
        end;
    end;
    if (coef ~= 1)
        coef =  norm(B, norm_no) / (1 - norm(B, norm_no)) ;
    end;
     
    k = 1;
    x0 = c;
    xk = B * x0 + c;
    
    converge = true;
    while (norm(xk - x0, norm_no) * coef >= eps )
       x0 = xk;
       xk = B * x0 + c;
       k = k + 1;
       for i = 1 : size(xk)
           if (isnan(xk(i)) || isinf(xk(i)))
               converge = false;
               break;
           end;
       end;
       if (converge == false || (coef == 1 && k == max_iterations))
           converge = false;
           break;
       end;
    end;
    
    fprintf(fileId, '\tX when t = %.4f, k = %d\n', t, k);
    if (converge)
        fprintf(fileId, '%.4f\n', xk);
    else
        fprintf(fileId, 'Converge == false');
    end;
    fprintf(fileId, '\n\n');
    
    if (k < best_k && converge == true)
        best_k = k;
        best_t = t;
        x = xk;
    end;
    
end;

converge = best_k < Inf;
if (converge)
    closure = A * x - b;
else
    closure = Inf;
    x = Inf;
end;

fclose(fileId);

end

